package ws;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.AndroidHttpTransport;

import android.util.Log;

public class EmpresaWS {
	private static String METHOD_NAME ;
	private static String NAMESPACE ;
	private static String URL;

	/*
	 * 
	 */
	public void coneccionws(){

		boolean londres = true;

		if(londres){
			NAMESPACE = "http://ws/"; 
			URL = "http://172.22.4.108:8080/Libro2SOA/services/EmpresaSOA?wsdl";

			//	Toast.makeText(null, "servidor", Toast.LENGTH_SHORT).show();
			//		Toast.makeText(getBaseContext(),respuesta, 
			//				Toast.LENGTH_SHORT).show();
		}else{
			NAMESPACE = "http://ws/";
			URL = "http://172.22.4.108:8080/Libro2SOA/services/EmpresaSOA?wsdl";
		}
	}

	public String findempresas(){
		coneccionws();

		METHOD_NAME = "findempresa";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
//		PropertyInfo findempresa = new PropertyInfo();
//		findempresa.setType(String.class);
//		request.addProperty(findempresa);
		//Log.i("log-android","ha llegado aqui ws--"+findempresa);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);


		String datosJsonString;
		try
		{

			aht.call("", soapEnvelope);
			Log.i("log-android","ha llegado aquiiii probando--"+URL);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

			datosJsonString =resultString.toString();

			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}
	public String findlibros(int idEmpresa){
		coneccionws();

		METHOD_NAME = "findlibro";
		String SOAP_ACTION = NAMESPACE + METHOD_NAME;

		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		PropertyInfo findlibro1 = new PropertyInfo();

		findlibro1.setName("idEmpresa");
		findlibro1.setValue(idEmpresa);
		findlibro1.setType(String.class);

		request.addProperty(findlibro1);
		//Log.i("log-android","ha llegado aqui ws--"+findempresa);

		SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
		soapEnvelope.dotNet = true;
		soapEnvelope.implicitTypes =true;
		soapEnvelope.setOutputSoapObject(request);

		AndroidHttpTransport aht = new AndroidHttpTransport(URL);


		String datosJsonString;
		try
		{

			aht.call("", soapEnvelope);
			Log.i("log-android","ha llegado aquiiii probando--"+URL);
			SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();

			datosJsonString =resultString.toString();

			return datosJsonString;
		}
		catch (Exception e)
		{
			e.printStackTrace();
			// en caso de no tener acceso al web service
			datosJsonString = "Error Conexion";}
		return datosJsonString;


	}

}

